package com.usian.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.R;
import com.usian.mapper.EmpMapper;
import com.usian.mapper.PostsMapper;
import com.usian.pojo.Emp;
import com.usian.pojo.Posts;
import com.usian.service.PostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/posts")
public class PostsController {
    @Autowired
    private PostsService postsService;

    @Autowired
    private PostsMapper postsMapper;

    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(HttpSession session, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer pageSize, String name){
        Page<Posts> page1=postsService.findPage(session,page,pageSize,name);
        return R.success(page1);
    }

    /**
     * 添加
     * @param posts
     * @return
     */
    @PostMapping
    public R add(@RequestBody Posts posts,HttpSession session){
        if (posts.getName().equals("董事长")){
            return R.error("董事长已存在！");
        }
        Integer count=postsMapper.findByName(posts.getName(),posts.getDid());
        if (count>0){
            return R.error("该部门已有该职位！");
        }
        postsService.add(posts,session);
        return R.success("添加成功");
    }

    /**
     * 查询集合
     * @param val
     * @return
     */
    @GetMapping("/{val}")
    public R findByList(@PathVariable Integer val){
        List<Posts> list=postsService.findByList(val);
        return R.success(list);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id){
        Posts posts=postsService.findById(id);
        return R.success(posts);
    }

    /**
     * 修改
     * @param posts
     * @param session
     * @return
     */
    @PutMapping
    public R edit(@RequestBody Posts posts,HttpSession session){
        String name=postsMapper.findByName2(posts.getId());
        Integer count=postsMapper.findCount(posts.getId());
        if (!posts.getName().equals(name)){
            if (count!=0){
                return R.error("该职位有员工，无法直接修改！");
            }
            if (posts.getName().equals("董事长")){
                return R.error("董事长已存在！");
            }
        }
        Integer count1=postsMapper.findByName1(posts.getId(),posts.getName(),posts.getDid());
        if (count1>0){
            return R.error("该部门已有该职位！");
        }
        Integer id=(Integer) session.getAttribute("login");
        Emp emp=empMapper.findById(id);
        if (emp.getGrade()==0){
            posts.setCode(posts.getCode()-1);
        }
        postsMapper.updateById(posts);
        return R.success("修改成功");
    }

    /**
     * 批删 - 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(String[] ids){
        List<String> list = Arrays.asList(ids);
        for (String id : ids) {
            Integer count=postsMapper.findByEname(Integer.valueOf(id));
            if (count!=0){
                return R.error("所选职位下有员工，不能直接删除！");
            }
        }
        postsMapper.deleteBatchIds(list);
        return R.success("删除成功");
    }
}












